﻿using H.Hands;
using H.Types;
using H.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace H.KML.Mouses
{
    public abstract class KMMouse : IMouse
    {
        /// <summary>
        /// 内部管理的鼠标实例
        /// </summary>
        protected readonly KMHand _Hand;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="hand"></param>
        protected KMMouse(KMHand hand)
        {
            _Hand = hand;
        }

        ///<inheritdoc/>
        public abstract bool Down();

        ///<inheritdoc/>
        public abstract bool Up();

        ///<inheritdoc/>
        public abstract bool Click();

        ///<inheritdoc/>
        public virtual bool Click(Pos pos)
        {
            if (_Hand.MoveTo(pos))
            {
                return Click();
            }
            else
            {
                return false;
            }
        }

        ///<inheritdoc/>
        public virtual bool Click(Rect rect)
        {
            Pos point = Pos.Empty;
            _Hand.MousePoint(ref point);

            if (rect.Intersect(point))
            {
                return Click();
            }
            else
            {
                return Click(rect.RandomPoint());
            }
        }

        ///<inheritdoc/>
        public virtual bool DoubleClick()
        {
            try
            {
                if (Down())
                {
                    DelayUtil.Delay(100, 120);
                    Up();
                    DelayUtil.Delay(80, 100);
                    Down();
                    return true;
                } else
                {
                    return false;
                }
            } finally
            {
                Up();
            }
        }

        ///<inheritdoc/>
        public bool DoubleClick(Pos pos)
        {
            if (_Hand.MoveTo(pos))
            {
                return DoubleClick();
            }
            else
            {
                return false;
            }
        }

        ///<inheritdoc/>
        public bool DoubleClick(Rect rect)
        {
            Pos point = Pos.Empty;
            _Hand.MousePoint(ref point);

            if (rect.Intersect(point))
            {
                return DoubleClick();
            }
            else
            {
                return DoubleClick(rect.RandomPoint());
            }
        }


    }
}
